package cn.com.git.example.common.util.object;

import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;

import java.util.Collection;
import java.util.List;

/**
 * @author zhangzhenfu
 * @Description
 * @date 2020/4/3 15:26
 */
public class MapperFacadeUtils {

    private static MapperFacade mapperFacade = null;

    static {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        mapperFacade = mapperFactory.getMapperFacade();
    }

    /**
     * 目标对象与转换的对象属性名称相同
     *
     * @param source 源对象
     * @param destn  终对象
     */
    public static void copy(final Object source, final Object destn) {
        mapperFacade.map(source, destn);
    }

    /**
     * 基于mapperFacade转换对象的类型.
     */
    public static <T> T map(Object source, Class<T> destinationClass) {
        return mapperFacade.map(source, destinationClass);
    }

    /**
     * 基于Dozer转换Collection中对象的类型.
     */
    @SuppressWarnings("rawtypes")
    public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass) {
        return mapperFacade.mapAsList(sourceList, destinationClass);
    }

}